package com.uchengwang.ui.cm_ui.me.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.uu.bean.NoticeBean;
import com.uchengwang.ui.cm_ui.uu.bean.NoticeListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.SPUtils;

import java.util.List;

/**
 * Created by U城网 on 2017/11/3.
 * 个人用户相关操作
 */

public class HttpUserManager {
    private static HttpUserManager manager;
    private HttpUserManager (){}

    public static HttpUserManager getManager() {
        if (manager == null){
            manager = new HttpUserManager();
        }
        return manager;
    }

    /**
     * 修改用户信息
     * */
    public void changeUserInfo(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(UserConfig.ChangeInfo.method_change, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 用老token获取新token
     * */
    public void getNewToken(final Activity activity, final OnResultListener listener){
        String token  = SPUtils.getPreferences(activity).getToken();
        //token有效过期
        if (!TextUtils.isEmpty(token)){
            MapRequest map = new MapRequest();
            HttpManager.newInstance(activity).query(UserConfig.GetNewToken.method, UserConfig.GetNewToken.URL, map, new HttpCallBack() {
                @Override
                public void onError(int status, String msg) {
                   Log.d("path", "获取新token stutas="+status+"=msg="+msg);
                    listener.onResult(status, msg, 0, null);
               }

             @Override
              public void onResp(int status, String msg, String data) {
                 Log.d("path", "获取新token stutas="+status+"=msg="+msg+"=data="+data);

                 if (status ==0 && !TextUtils.isEmpty(data)){
                     JSONObject jsonObject = JsonUtils.getManager().getJSONObject(data);
                     String newToken = jsonObject.getString(BaseConfig.TOKEN);
                     SPUtils.getPreferences(activity).setToken(newToken);
                     SPUtils.getPreferences(activity).setTokenTime();
                     UChengApplication.TOKEN = newToken;
                 }
                 listener.onResult(status, msg, 0, null);
             }
         });
        }else {
            listener.onResult(0, null, 0, null);
        }
    }

    /**
     * 获取我的系统通知
     * @param updateRead 如果该字段为true,当调用该接口后,被获取的数据就会被标记为已读
     * */
    public void getMyNotice(Activity activity, int page, int perPage, Boolean isRead, boolean updateRead, final OnHttpResultListener<NoticeBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        if (isRead != null){
            map.put(BaseConfig.IS_READ, isRead);
        }
        map.put(BaseConfig.UPDATE_READ, updateRead);

        HttpManager.newInstance(activity).query(UserConfig.GetMyNotice.mrthod, UserConfig.GetMyNotice.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {

                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    NoticeListBean bean = JsonUtils.getManager().stringToBean(data, NoticeListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 根据手机通讯录的好友 查看注册问题
     * */
    public void getContacts(Activity activity, String[] contacts, final OnHttpResultListener<UserBean> listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.MOBILE_LIST, contacts);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.POST, UserConfig.USER_MOBILE_LIST, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 && !TextUtils.isEmpty(data)){
                    List<UserBean> list = JsonUtils.getManager().parseList(data, UserBean.class);
                    listener.onResult(status, msg, list.size(), list);
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 获取我的新粉丝数
     * */
    public void getMyFans(Activity activity, Boolean updateRead, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UPDATE_READ, updateRead);

        HttpManager.newInstance(activity).queryCheck(IHttpManager.RequestMethod.GET, "/user/newfans", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("hx", "我的心粉丝msg=="+msg+"=data="+data);
                int newFans = 0;
                if (status ==0 && !TextUtils.isEmpty(data)){
                    JSONObject jsonObject = JsonUtils.getManager().getJSONObject(data);
                    newFans = jsonObject.getIntValue("newFans");
                }
                listener.onResult(status, msg, newFans, null);
            }
        });
    }

    /**
     * 获取我的新票劵
     * */
    public void getMyTickets(Activity activity, Boolean updateRead, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UPDATE_READ, updateRead);

        HttpManager.newInstance(activity).queryCheck(IHttpManager.RequestMethod.GET, "/act/newticket", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("hx", "我的心票劵msg=="+msg+"=data="+data);
                int newTicket = 0;
                if (status ==0 && !TextUtils.isEmpty(data)){
                    JSONObject jsonObject = JsonUtils.getManager().getJSONObject(data);
                    newTicket = jsonObject.getIntValue("newTicket");
                }
                listener.onResult(status, msg, newTicket, null);
            }
        });
    }

    /**
     * 举报个人
     * */
    public void reportPeople(Activity activity, long uid, String desc, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.REPORT_UID, uid);
        map.put(BaseConfig.DESCRIPTION, desc);

        HttpManager.newInstance(activity).queryCheck(UserConfig.ReportPer.method, UserConfig.ReportPer.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("hx", "我的心票劵msg=="+msg+"=data="+data);
                listener.onResult(status, msg, 0, null);
            }
        });
    }

}
